/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.wteam.dragon.system.config.swagger;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * swagger配置页面
 *
 * @Author 陈晓辉
 * @Date 2020/5/29 17:15
 */
@Configuration
@EnableSwagger2
@RequiredArgsConstructor
public class SwaggerConfig {
    @Value("${jwt.header}")
    private String tokenHeader;

    @Value("${swagger.enabled}")
    private Boolean enabled;

    @Bean
    @SuppressWarnings("all")
    public Docket createSysRestApi() {
        Predicate<RequestHandler> basePackageOne = RequestHandlerSelectors.basePackage("com.wteam.dragon.system");
        Predicate<RequestHandler> basePackageTwo = RequestHandlerSelectors.basePackage("com.wteam.dragon.model");
        return defaultDocket()
                .select()
                .apis(Predicates.or(basePackageOne, basePackageTwo))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .groupName("系统");
    }

    @Bean
    @SuppressWarnings("all")
    public Docket createRestApi() {
        Predicate<RequestHandler> basePackageOne = RequestHandlerSelectors.basePackage("com.wteam.dragon.system");
        Predicate<RequestHandler> basePackageTwo = RequestHandlerSelectors.basePackage("com.wteam.dragon.model");
        return defaultDocket()
                .select()
                .apis(Predicates.not(Predicates.or(basePackageOne, basePackageTwo)))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .groupName("业务");
    }

    private Docket defaultDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .apiInfo(apiInfo())
                .directModelSubstitute(Timestamp.class, Date.class)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .groupName("系统");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .description("网维18届后台管理系统1.3 后台管理框架")
                .title("接口文档")
                .version("1.3")
                .build();
    }

    private List<ApiKey> securitySchemes() {
        return Collections.singletonList(new ApiKey("Authorization", "Authorization", tokenHeader));
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.any()).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
}
